home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_360 / uucp / uucp0.lzh / src / lib / config.c < prev    next >
C/C++ Source or Header  |  1990-05-19  |  3KB  |  175 lines

  1.  
  2. /*
  3.  *  CONFIG.C
  4.  *
  5.  *  $Header: Beta:src/uucp/src/lib/RCS/config.c,v 1.1 90/02/02 12:08:37 dillon Exp Locker: dillon $
  6.  *
  7.  *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
  8.  *
  9.  *  Extract fields from UULIB:Config
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <fcntl.h>
  15. #include "config.h"
  16.  
  17. Prototype char *FindConfig(const char *);
  18. Prototype char *GetConfig(const char *, char *);
  19. Prototype char *GetConfigDir(char *);
  20. Prototype char *GetConfigProgram(char *);
  21. Prototype char *MakeConfigPath(const char *, const char *);
  22. Prototype char *MallocEnviro(const char *);
  23. Prototype FILE *openlib(const char *);
  24. Prototype FILE *openlib_write(const char *);
  25.  
  26. #define CTLZ    ('z'&0x1F)
  27.  
  28. static char *ConfBuf = NULL;
  29.  
  30. char *
  31. FindConfig(field)
  32. const char *field;
  33. {
  34.     char *str;
  35.     short flen = strlen(field);
  36.  
  37.     if (ConfBuf == NULL) {
  38.     FILE *fi;
  39.     fi = fopen("S:UUConfig", "r");
  40.     if (fi == NULL)
  41.         fi = fopen("UULIB:Config", "r");
  42.     if (fi) {
  43.         long buflen;
  44.         fseek(fi, 0L, 2);
  45.         buflen = ftell(fi);
  46.         fseek(fi, 0L, 0);
  47.         if (buflen > 0 && (ConfBuf = malloc(buflen + 1))) {
  48.         fread(ConfBuf, buflen, 1, fi);
  49.         ConfBuf[buflen] = CTLZ;     /*    can't use \0 */
  50.         for (str = ConfBuf; *str; ++str) {
  51.             char *bup;
  52.             if (*str == '\n') {     /*  make separate strs */
  53.             *str = 0;
  54.                         /*    remove white space at end */
  55.             for (bup = str - 1; bup >= ConfBuf && (*bup == ' ' || *bup == 9); --bup)
  56.                 *bup = 0;
  57.             }
  58.         }
  59.         } else {
  60.         ConfBuf = NULL;
  61.         }
  62.     } else {
  63.         fprintf(stderr, "Couldn't open S:UUConfig or UULIB:Config\n");
  64.     }
  65.     }
  66.     if (ConfBuf == NULL)
  67.     return(NULL);
  68.     /*
  69.      *    Search ConfBuf for Field<space/tab>
  70.      */
  71.  
  72.     for (str = ConfBuf; *str != CTLZ; str += strlen(str) + 1) {
  73.     if (*str == 0 || *str == '#')
  74.         continue;
  75.     if (strncmp(str, field, flen) == 0 && (str[flen] == ' ' || str[flen] == '\t')) {
  76.         str += flen;
  77.         while (*str == ' ' || *str == 9)
  78.         ++str;
  79.         return(str);
  80.     }
  81.     }
  82.     return(NULL);
  83. }
  84.  
  85. char *
  86. GetConfig(field, def)
  87. const char *field;
  88. char *def;
  89. {
  90.     char *result = FindConfig(field);
  91.  
  92.     if (result == NULL)
  93.     result = def;
  94.     return(result);
  95. }
  96.  
  97. char *
  98. GetConfigDir(field)
  99. char *field;
  100. {
  101.     char *result = FindConfig(field);
  102.  
  103.     if (result == NULL)
  104.     result = field + strlen(field) + 1;
  105.     return(result);
  106. }
  107.  
  108. char *
  109. GetConfigProgram(field)
  110. char *field;
  111. {
  112.     char *result = FindConfig(field);
  113.     if (result == NULL)
  114.     result = field;
  115.     return(result);
  116. }
  117.  
  118. char *
  119. MakeConfigPath(field, trailer)
  120. const char *field;
  121. const char *trailer;
  122. {
  123.     static char Buf[512];
  124.     char *result = GetConfigDir(field);
  125.     short len = strlen(result) - 1;
  126.  
  127.     if (len > 0 && result[len] == '/' || result[len] == ':')
  128.     sprintf(Buf, "%s%s", result, trailer);
  129.     else
  130.     sprintf(Buf, "%s/%s", result, trailer);
  131.     return(Buf);
  132. }
  133.  
  134. char *
  135. MallocEnviro(envname)
  136. const char *envname;
  137. {
  138.     int fd;
  139.     int len;
  140.     char *tmp = malloc(strlen(envname) + 32);
  141.  
  142.     sprintf(tmp, "ENV:%s", envname);
  143.     fd = open(tmp, O_RDONLY);
  144.     free(tmp);
  145.  
  146.     if (fd < 0)
  147.     return(NULL);
  148.     len = lseek(fd, 0L, 2);
  149.     lseek(fd, 0L, 0);
  150.     if (len >= 0) {
  151.     tmp = malloc(len + 1);
  152.     read(fd, tmp, len);
  153.     tmp[len] = 0;
  154.     } else {
  155.     tmp = NULL;
  156.     }
  157.     close(fd);
  158.     return(tmp);
  159. }
  160.  
  161. FILE *
  162. openlib(filename)
  163. const char *filename;
  164. {
  165.     return (fopen(MakeConfigPath(UULIB, filename), "r"));
  166. }
  167.  
  168. FILE *
  169. openlib_write(filename)
  170. const char *filename;
  171. {
  172.     return (fopen(MakeConfigPath(UULIB, filename), "w"));
  173. }
  174.  
  175.